using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

namespace FrameWork.TileMapManager
{
    public class CropView : MonoBehaviour
    {
        [SerializeField] private Tilemap plantCropTilemap;
        [SerializeField] private TileBase clearTile;
        
        /// <summary>
        /// 加载地图时候调用
        /// 显示所有可耕种的瓦片图片
        /// </summary>
        /// <param name="cultivatedTiles">可耕种的瓦片集合</param>
        public void PlantCropTileViewInit(MapModel model)
        {
            if(model.CropDatasDicTiles.Count < 1)
                return;
            foreach (var value in model.CropDatasDicTiles)
            {
                plantCropTilemap.SetTile(value.Key, value.Value.GetCropSprite());
            }
        }
        
        /// <summary>
        /// 更新UI界面
        /// </summary>
        /// <param name="pos">开垦的瓦片地图坐标</param>
        public void UpdateView(Vector3Int pos, CropData cropData)
        {
            // print("开垦土地" + pos);
            plantCropTilemap.SetTile(pos, cropData.GetCropSprite());
            print(cropData.GetCropSprite());
        }
        
        /// <summary>
        /// 清除图片
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="cropData"></param>
        public void CelarView(Vector3Int pos)
        {
            // print("开垦土地" + pos);
            plantCropTilemap.SetTile(pos, clearTile);
        }

        /// <summary>
        /// 坐标转换，世界坐标转换为瓦片地图坐标
        /// </summary>
        /// <param name="worldPosition"></param>
        /// <returns></returns>
        public Vector3Int WorldToCell(Vector3 worldPosition)
        {
            return plantCropTilemap.WorldToCell(worldPosition);
        }
    }
}
